package q841_canVisitAllRooms;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution_2 {
    /*
    不需要额外统计层数（size）的BFS
    只要有一个房间有钥匙且没被进入 就进入队列 最后队列仍然必清空
     */
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] vis = new boolean[rooms.size()];
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.offer(0);
        while (!queue.isEmpty()) {
            int curRoom = queue.poll();
            vis[curRoom] = true;
            for (Integer room : rooms.get(curRoom)) {
                if (!vis[room]) queue.offer(room);
            }
        }
        for (boolean b : vis) {
            if (!b) return false;
        }
        return true;
    }
}
